
####################################################################
###################################################################
#CIRCUIT JUDGES

#read in dataset with circuit-year as unit of analysis, and indicator for
    #active and senior judges on circuit that is 1 if they are a dem and 0 if gop.

judges <- read.dta("circuit_judges_by_year_party_only.dta", convert.underscore=T)

#for now, keep only 1925-2004, to match up with songer and sunstein data.

judges <- judges[judges$year>=1925 & judges$year<=2004,] #drop 1891 since only 1 judge in many cases
detach()
attach.all(judges, overwrite=T)
circuit.year <- paste(circuit, year)
circuit <- ifelse(circuit == 0, 12, circuit)#recode DC circuit
unique.circuit.year <- unique(circuit.year)
unique.circuit <- unique(circuit)
unique.year <- unique(year)
loop1 <- length(circuit.year)

#29 active judge slots (with many missing in many circ-years)
#24 senior judge slots (with many missing in many circ-years)
    #note that DC circuit is coded as "0"

number.seats <- judgeships.authorized#number of authorized seats by circuit

#transpose data to ease calculations
#active.tranpose <- data.frame(t(judges[1:32]))

#first, get total number of active and senior judges by circuit year
    #j1dem:j29dem appear in columns 4:32 
    #also get number of dems
xx <- 4
yy <- 32
#pull out for easier calculation
active.judges.matrix <- judges[xx:yy]
active.judges.circyear <-  rowSums(ifelse(is.na(active.judges.matrix), 0, 1)) 
active.dems.circyear <-  rowSums(active.judges.matrix, na.rm=T)
active.gop.circyear <- active.judges.circyear - active.dems.circyear
#senior1.dem:senior24.dem appear in columbs 33:56 DOUBLE CHECK THIS !!
rr <- 33
tt <- 56
senior.judges.matrix <- judges[rr:tt]
senior.judges.circyear <-  rowSums(ifelse(is.na(senior.judges.matrix), 0, 1)) 
senior.dems.circyear <-  rowSums(senior.judges.matrix, na.rm=T)
senior.gop.circyear <- senior.judges.circyear - senior.dems.circyear

total.judges.circyear <- active.judges.circyear + senior.judges.circyear
total.dems.circyear <- active.dems.circyear + senior.dems.circyear
total.gop.circyear <- active.gop.circyear + senior.gop.circyear

dem.prop.active.circyear <- active.dems.circyear/active.judges.circyear
dem.prop.senior.circyear <- senior.dems.circyear/senior.judges.circyear
dem.prop.total.circyear <- total.dems.circyear/total.judges.circyear

#build yearly meaures
    #including only active judges
loop <- length(unique.year)
    
number.seats.year <- rep(NA, loop)#number of authorized seats per year
active.judges.year <- rep(NA, loop)#number of active judges per year
active.dems.year <- rep(NA, loop)
active.gop.year <- rep(NA, loop)

    #senior judges
senior.judges.year <- rep(NA, loop)#number of senior judges per year
senior.dems.year <- rep(NA, loop)
senior.gop.year <- rep(NA, loop)
  
    #total combined
total.judges.year <- rep(NA, loop)
total.dems.year <- rep(NA, loop)
total.gop.year <- rep(NA, loop)


for (i in 1:loop){
  keep <- year == unique.year[i]
  number.seats.year[i] <- sum(judgeships.authorized[keep])
  active.judges.year[i] <- sum(active.judges.circyear[keep])
  active.dems.year[i] <-  sum(active.dems.circyear[keep])
  active.gop.year[i] <- sum(active.gop.circyear[keep])
  senior.judges.year[i] <- sum(senior.judges.circyear[keep])
  senior.dems.year[i] <- sum(senior.dems.circyear[keep])
  senior.gop.year[i] <- sum(senior.gop.circyear[keep])
  total.judges.year[i] <- sum(total.judges.circyear[keep])
  total.dems.year[i] <- sum(total.dems.circyear[keep])
  total.gop.year[i] <- sum(total.gop.circyear[keep])
  }

active.dems.per.year <- active.dems.year/(active.dems.year+active.gop.year)
senior.dems.per.year <- senior.dems.year/(senior.dems.year+senior.gop.year)    
total.dems.per.year <- total.dems.year/(total.dems.year+total.gop.year)

year.matrix <- data.frame(year = unique.year,  number.seats.year = number.seats.year ,  active.judges.year = active.judges.year,  
  active.dems.year = active.dems.year,  active.dems.per.year = active.dems.per.year,  
   senior.judges.year = senior.judges.year,  senior.dems.year = senior.dems.year,  senior.dems.per.year = senior.dems.per.year,   
   total.judges.year = total.judges.year,  total.dems.year = total.dems.year,  total.dems.per.year = total.dems.per.year)


#use these commands to export summary info to a dataset.
partisan.composition <- data.frame(year, circuit, number.seats, active.judges.circyear,  
  active.dems.circyear, active.gop.circyear, dem.prop.active.circyear, senior.judges.circyear, dem.prop.senior.circyear, dem.prop.total.circyear, 
  senior.judges.circyear, senior.dems.circyear, senior.gop.circyear, total.judges.circyear, total.dems.circyear,total.gop.circyear )
write.csv(partisan.composition, "partisan.composition.csv")
  

